<template>
    <section>
        <el-dialog title="编辑" :visible.sync="visible" :close-on-click-modal="false"  width="40%">
            <el-form label-width="120px" ref="form" :model="form" :rules="dataRule">
            <#list slave.columnList as item >
                <#if item.name!="id">
                    <#if item.name!=parentKeyCamel>
                    <el-form-item label="${item.comment}"  prop="${item.name}">
                        <el-input v-model="form.${item.name}"  placeholder="${item.comment}"></el-input>
                    </el-form-item>
                    <#else>
                     <el-form-item label="${item.comment}"  prop="${item.name}">
                        <el-select v-model="form.${item.name}" placeholder="${item.comment}">
                            <el-option v-for="item in records"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                            </el-option>
                        </el-select>
                       </el-form-item>  
                    </#if>
                </#if>
            </#list>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="visible = false">取消</el-button>
                <el-button type="primary" @click.native="submit" :loading="loading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>
<script>
    export default {
        data() {
            return {
                visible: false,
                loading: false,
                form: {
        <#list slave.columnList as item >
        ${item.name}:'',
        </#list>
             },
              records: [],
            dataRule: {
            <#list slave.columnList as item >
            ${item.name}: [{
                required: true,
                message: '${item.comment}不能为空',
                trigger: 'blur'
            }],
            </#list>

            }
        }
        },
        methods: {
            init(formData) {
                // this.$refs['form'].reset()
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['form'].resetFields()
                if (formData) {
                    this.form = formData
                }else{
                    this.form={}
                }
            })

            },
             fetchList() {
            this.loading = true
            this.$http
                .get('/api/${master.camelName}/list', {
                params: {
                    curPage: 1,
                    pageSize: 500
                }
                })
                .then(({
                data
                }) => {
                this.loading = false
                this.records = data.records
                })
            },
            submit() {
                this.$refs['form'].validate((valid) => {
                    if (!valid) {
                    console.log('error submit!!')
                    return false
                } else {
                    this.loading = true
                    this.$http.post('/api/${slave.camelName}', this.form)
                            .then((data) => {
                        this.$message({
                        message: '操作成功',
                        type: 'success',
                        duration: 1000,
                        onClose: () => {
                        this.loading = false;
                    this.visible = false
                    this.$emit('refreshDataList')
                }
                })
                })
                .catch(e => {
                        this.loading = false;
                    console.log(e)
                    this.$message.error(e)
                })
                }
            });
            }
        },
         mounted() {
            this.fetchList()
        }
    }
</script>